package com.ganinfo.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ganinfo.common.Result;
import com.ganinfo.common.controller.BaseController;
import com.ganinfo.common.data.ParmaterUtils;
import com.ganinfo.common.data.TransmitMap;
import com.ganinfo.common.interceptor.shiro.MyRealm;
import com.ganinfo.sys.model.SysPermission;
import com.ganinfo.sys.service.ISysPermissionService;

/**
 * @ClassName:   SysPermissionController
 * @Description: [sys_permission]控制类
 * @author:      ruikanwang
 * @date:        
 */
@Controller
public class SysPermissionController extends BaseController {

    private final ISysPermissionService service;

    @Autowired
    public SysPermissionController(ISysPermissionService service) {
        this.service = service;
    }
    @Autowired
	private MyRealm customRealm;
	
	@RequestMapping("login")
	public String login(HttpServletRequest request)throws Exception{
		
		//如果登陆失败从request中获取认证异常信息，shiroLoginFailure就是shiro异常类的全限定名
		String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
		//根据shiro返回的异常类路径判断，抛出指定异常信息
		if(exceptionClassName!=null){
			if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
				//最终会抛给异常处理器
				throw new Exception("账号不存在");
			} else if (IncorrectCredentialsException.class.getName().equals(
					exceptionClassName)) {
				throw new Exception("用户名/密码错误");
			} else if("randomCodeError".equals(exceptionClassName)){
				throw new Exception("验证码错误 ");
			}else {
				throw new Exception();//最终在异常处理器生成未知错误
			}
		}
		//此方法不处理登陆成功（认证成功），shiro认证成功会自动跳转到上一个请求路径
		//登陆失败还到login页面
		return "login";
	}

    /**
     * @Title:       getSysPermissionList
     * @Description: 分页获取列表
     * @param：                    pageSize(每页记录数) pageNumber(页数) orderby(排序字段) orderbymethod(排序方式)
     * @return
     * @return:      String
     */
    @RequestMapping(value="/getSysPermissionList",method=RequestMethod.GET,produces={"application/json;charset=UTF-8"})
    @ResponseBody
    public Result getSysPermissionList(SysPermission paramSysPermission) {
    	Result returndate=null;
    	TransmitMap transmitMap = this.getTransmitMap();
    	List<String> paramCheckList = ParmaterUtils.paramCheck(transmitMap,"pageSize","pageNumber");
    	if(paramCheckList.size() > 0){
    		returndate=this.renderParamError(paramCheckList);
		}else{
			try {
				returndate=this.renderSuccess(service.getSysPermissionList(transmitMap));
			} catch (Exception e) {
				returndate=this.renderError();
				e.printStackTrace();
			}
		}
        return returndate;
    }
    
    /**
     * @Title:       addSysPermission
     * @Description: 添加
     * @param 
     * @return
     * @return:      String
     */
    @RequestMapping(value="/addSysPermission",method=RequestMethod.POST,produces={"application/json;charset=UTF-8"})
    @ResponseBody
    public Result addSysPermission(SysPermission paramSysPermission) {
    	Result returndate=null;
    	try {
    		if(service.addSysPermission(paramSysPermission)){
    			returndate=this.renderSuccess(null);
    		}else{
    			returndate=this.renderError();
    		}
		} catch (Exception e) {
			returndate=this.renderError();
			e.printStackTrace();
		}
        return returndate;
    }
    
    /**
     * @Title:       updateSysPermission
     * @Description: 更新
     * @param        paramSysPermission
     * @return
     * @return:      String
     */
    @RequestMapping(value="/updateSysPermission",method=RequestMethod.POST,produces={"application/json;charset=UTF-8"})
    @ResponseBody
    public Result updateSysPermission(SysPermission paramSysPermission) {
    	Result returndate=null;
    	try {
    		if(service.updateSysPermission(paramSysPermission)){
    			returndate=this.renderSuccess(null);
    		}else{
    			returndate=this.renderError();
    		}
		} catch (Exception e) {
			returndate=this.renderError();
			e.printStackTrace();
		}
        return returndate;
    }
    
    /**
     * @Title:       deleteSysPermission
     * @Description: 删除
     * @param paramSysPermission
     * @return
     * @return:      String
     */
    @RequestMapping(value="/deleteSysPermission",method=RequestMethod.POST,produces={"application/json;charset=UTF-8"})
    @ResponseBody
    public Result deleteSysPermission(SysPermission paramSysPermission) {
    	Result returndate=null;
    	try {
    		if(service.deleteSysPermission(paramSysPermission)){
    			returndate=this.renderSuccess(null);
    		}else{
    			returndate=this.renderError();
    		}
		} catch (Exception e) {
			returndate=this.renderError();
			e.printStackTrace();
		}
        return returndate;
    }
}
